﻿#region Using

using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Enum;
using RuleCode.Table;

#endregion

namespace map.manage
{
    public partial class area5 : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            InitMap();
            if (!IsPostBack)
            {
                Literal_map.Text = RuleCode.Util.getmapurl();
                //bind3();
                //GridViewBind();
                Bind0();
                Button2.Visible = false;
            }
        }

        private void Bind0()
        {
            string sheng = zfunction.Province;
            县 x = 县.Find(" 省编码 = '" + sheng + "' ");
            DropDownList1.Items.Clear();
            DropDownList2.Items.Clear();
            DropDownList3.Items.Clear();
            // DropDownList7.Items.Clear();
            foreach (DataRow item in x.MyTable.Rows)
            {
                DropDownList1.Items.Add(item["县编码"].ToString());
            }

            if (x.Count > 0)
            {
                Bind1();
            }
            else
            {
                //todo:用geocode显示该县
                string provice = zfunction.Province;
                string county = DropDownList1.SelectedValue;
                string query = provice + "," + county;
                string scriptTag = "showAddress('" + query + "');";
                ScriptManager.RegisterStartupScript(
                    UpdatePanel1, UpdatePanel1.GetType(), "bodyevent3", scriptTag, true);
            }
        }

        /// <summary>
        /// 绑定区
        /// </summary>
        private void Bind1()
        {
            string xian = DropDownList1.Text;
            区 q = 区.Find(" 县编码 = '" + xian + "' ");
            DropDownList2.Items.Clear();
            DropDownList3.Items.Clear();
            //DropDownList7.Items.Clear();
            foreach (DataRow item in q.MyTable.Rows)
            {
                DropDownList2.Items.Add(item["区编码"].ToString());
            }
            if (q.Count > 0)
            {
                Bind2();
            }
            else
            {
                区 q1 = 区.Find(" 区编码 = '" + DropDownList2.Text + "' ");
                markpos2(q1.taobaolat.ToString(), q1.taobaolong.ToString(), q1.区编码.ToString());
            }
        }

        /// <summary>
        /// 绑定区域
        /// </summary>
        private void Bind2()
        {
            区域 q = 区域.Find(" 区编码 = '" + DropDownList2.Text + "' "
                           + " and 县编码 = '" + DropDownList1.Text + "' ");
            DropDownList3.Items.Clear();

            foreach (DataRow item in q.MyTable.Rows)
            {
                DropDownList3.Items.Add(item["区域编码"].ToString());
            }
            //if (q.Count > 0)
            //{
            //    Bind4();
            //}


            markpos2(q.taobaolat.ToString(), q.taobaolong.ToString(), q.区域编码.ToString());

            GridViewBind();
        }

        private void InitMap()
        {
            string ScriptTag = "";


            ScriptTag =
                "<script type=\"text/javascript\">markpos(" + "116.39679908752441" + ","
                + "" + "39.917043746896674,'" + "2222" + "');</script>";

            ClientScript.RegisterStartupScript(GetType(), "bodyevent", ScriptTag);
        }

        /// <summary>
        /// 县
        /// </summary>
        private void bind1()
        {
            string sheng = zfunction.Province;
            县 x = 县.Find(" 省编码 = '" + sheng + "' ");
            DropDownList1.Items.Clear();
            foreach (DataRow item in x.MyTable.Rows)
            {
                DropDownList1.Items.Add(item["县编码"].ToString());
            }
            string ScriptTag = "";
            ScriptTag =
                "<script type=\"text/javascript\">markpos2(" + "116.39679908752441" + ","
                + "" + "39.917043746896674,'" + "2" + "');</script>";
            //ClientScript.RegisterStartupScript(GetType(), "bodyevent", ScriptTag);
            ScriptManager.RegisterStartupScript(
                Page, Page.GetType(), "bodyevent3", ScriptTag, false);
            //DropDownList1.DataBind();
        }

        /// <summary>
        /// 区
        /// </summary>
        private void bind2()
        {
            string xian = DropDownList1.Text;
            区 x = 区.Find(" 县编码 = '" + xian + "' ");
            DropDownList2.Items.Clear();
            foreach (DataRow item in x.MyTable.Rows)
            {
                DropDownList2.Items.Add(item["区编码"].ToString());
            }
            if (!IsPostBack)
            {
                string ScriptTag = "";
                if (x.taobaolong.Value == 0 || x.taobaolat.Value == 0)
                {
                    //ScriptTag =
                    //    "<script type=\"text/javascript\">markpos(" + "116.396" + ","
                    //    + "" + "39.917,'" + x.区编码.ToString() + "');</script>";
                    string provice = zfunction.Province;
                    string county = DropDownList1.SelectedValue;
                    string query = provice + "," + county;
                    ScriptTag = "showAddress('" + query + "');";
                }
                else
                {
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos2(" + x.taobaolong.ToString() + ","
                        + x.taobaolat.ToString() + ",'" + x.区编码.ToString() + "');</script>";
                }
                //ClientScript.RegisterStartupScript(GetType(), "bodyevent", ScriptTag);
                ScriptManager.RegisterStartupScript(
                    Page, Page.GetType(), "bodyevent3", ScriptTag, false);
                markpos2(x.taobaolat.ToString(), x.taobaolong.ToString(), x.区编码.ToString());
            }
        }

        /// <summary>
        /// 区域表格
        /// </summary>
        private void bind3()
        {
            区域 x = 区域.Find(" 区编码 = '" + DropDownList2.Text + "' "
                           + " and 县编码 = '" + DropDownList1.Text + "' ");
            DropDownList3.DataSource = x.MyTable.DefaultView;
            DropDownList3.DataTextField = "区域编码";
            DropDownList3.DataValueField = "区域编码";
            DropDownList3.DataBind();
            if (!IsPostBack)
            {
                string ScriptTag = "";
                if (x.taobaolong.Value == 0 || x.taobaolat.Value == 0)
                {
                    //ScriptTag =
                    //    "<script type=\"text/javascript\">markpos(" + "116.396" + ","
                    //    + "" + "39.917,'" + x.区编码.ToString() + "');</script>";
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos2(" + "116.39679908752441" + ","
                        + "" + "39.917043746896674,'" + x.区域编码.ToString() + "');</script>";
                }
                else
                {
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos2(" + x.taobaolong.ToString() + ","
                        + x.taobaolat.ToString() + ",'" + x.区域编码.ToString() + "');</script>";
                }
                //ClientScript.RegisterStartupScript(GetType(), "bodyevent", ScriptTag);
                //    ScriptManager.RegisterStartupScript(
                //this.Page, this.Page.GetType(), "bodyevent3", ScriptTag, false);
                markpos2(x.taobaolat.ToString(), x.taobaolong.ToString(), x.区域编码.ToString());
            }
        }


        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind1();
            Bind2();
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind2();
            ;
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            BizArea uc = BizArea.Find(" recid = '" + id + "'", "RECID");
            BizAreaImages i = BizAreaImages.Find(" BizAreaID = '" + id + "'", "BizAreaID");
            //while (i.Next())
            //{

            i.Delete(" BizAreaID = '" + id + "'");
            //}
            uc.Delete();
            GridViewBind();
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string id = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
            BizArea uc = BizArea.Find(" recid = '" + id + "'", "RECID");

            TextBox1.Text = uc.BizAreaName.ToString();
            TextBox_long.Text = uc.taobaolong.ToString();
            TextBox_lat.Text = uc.taobaolat.ToString();
            TextBox2.Text = uc.Description.ToString();
            Label_recid.Text = uc.RECID.ToString();
            owner.Text = uc.CreatedBy.ToString();
            owner.Enabled = true;
            if (uc.IsConfirmed == NoYes.Yes)
            {
                Button2.Visible = false;
            }
            else
            {
                Button2.Visible = true;
            }
            string ScriptTag = "";
            if (uc.taobaolong.Value == 0 || uc.taobaolat.Value == 0)
            {
                ScriptTag =
                    "markpos2(" + "" + "116.39679908752441,"
                    + "39.917043746896674" + ",'" + uc.BizAreaName.ToString() + "')";
            }
            else
            {
                ScriptTag =
                    "markpos2(" + uc.taobaolong.ToString() + ","
                    + uc.taobaolat.ToString() + ",'" + uc.BizAreaName.ToString() + "')";
            }
            //ScriptManager.RegisterStartupScript(
            //    this.UpdatePanel1, this.UpdatePanel1.GetType(), "bodyevent3", ScriptTag, true);
            markpos2(uc.taobaolat.ToString(), uc.taobaolong.ToString(), uc.BizAreaName.ToString());
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (DropDownList1.Text != "" && DropDownList2.Text != "")
            {
                //检查是否重复
                BizArea x = BizArea.Find(" 区编码 = '" + DropDownList2.Text + "' "
                                         + " and 县编码 = '" + DropDownList1.Text +
                                         "' and BizAreaName = '" + TextBox1.Text + "'");
                if (x.Count > 0)

                {
                    showError.Text = "该区域已存在！";
                }
                else
                {
                    BizArea uc = BizArea.Find(" 1=2 ");
                    uc.New();

                    uc.区编码.Value = DropDownList2.Text;
                    uc.县编码.Value = DropDownList1.Text;
                    uc.区域编码.Value = DropDownList3.Text;
                    uc.Description = TextBox2.Text;
                    uc.BizAreaName = TextBox1.Text;
                    uc.IsConfirmed = NoYes.No;
                    //todo:change after finished login function
                    //finished
                    uc.CreatedBy = AliApi.appUserId;

                    uc.taobaolong.Value = zfunction.str2double(TextBox_long.Text);
                    uc.taobaolat.Value = zfunction.str2double(TextBox_lat.Text);
                    uc.Insert();
                    GridViewBind();
                    showError.Text = "";
                }
            }
            else
            {
                MessageBox.Show(UpdatePanel1, "操作不成功.");
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            BizArea x = BizArea.Find(" recid = '" + Label_recid.Text + "'", "RECID");
            cbuser u1 = cbuser.Find("cbusernickname = '" + owner.Text + "' or cbuserid = '" + owner.Text + "'");
            if (u1.Count > 0)
            {
                x.BizAreaName.Value = TextBox1.Text;
                x.taobaolong.Value = zfunction.str2double(TextBox_long.Text);
                x.taobaolat.Value = zfunction.str2double(TextBox_lat.Text);
                x.Description = TextBox2.Text;
                x.CreatedBy = owner.Text;
                x.Modify();
                GridViewBind();
                showError.Text = "转让成功.";
            }
            else
            {
                showError.Text = "用户不存在.";
            }
        }

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            区域 q = 区域.Find(" 区编码 = '" + DropDownList2.Text + "' "
                           + " and 县编码 = '" + DropDownList1.Text + "' " + " and 区域编码 = '" + DropDownList3.Text + "' ");

            markpos2(q.taobaolat.ToString(), q.taobaolong.ToString(), q.区域编码.ToString());

            GridViewBind();
        }

        //changed:只能看到自己的，只能创建，不能删除，可修改
        private void GridViewBind()
        {
            BizArea x = BizArea.Find(" 区编码 = '" + DropDownList2.Text + "' "
                                     + " and 县编码 = '" + DropDownList1.Text + "' " +
                                     " and 区域编码 = '" + DropDownList3.Text + "' " +
                                     " and CreatedBy = '" + AliApi.appUserId + " '");
            GridView1.DataSource = x.MyTable;
            GridView1.DataBind();
        }

        private void markpos2(string taobaolat, string taobaolong, string name)
        {
            string ScriptTag = "";
            if (!IsPostBack)
            {
                if (taobaolong == "0" || taobaolat == "0")
                {
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos2(" + "116.39679908752441" + ","
                        + "" + "39.917043746896674,'" + name + "');</script>";
                }
                else
                {
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos2(" + taobaolong + ","
                        + taobaolat + ",'" + name + "');</script>";
                }
                ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                                                    "messagew", ScriptTag, false);
            }
            else
            {
                //改变了值的情况、
                ScriptTag = "markpos2(" + taobaolong + ","
                            + taobaolat + ",'" + name + "');";

                ScriptManager.RegisterStartupScript(
                    UpdatePanel1, UpdatePanel1.GetType(), "bodyevent3", ScriptTag, true);
            }
        }
    }
}